<?php
// $Id: DeliveryController.php 644 2013-01-20 10:21:08Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

class DeliveryController extends MemberController
{
    public $shopId;   //店铺ID
    public $shopInfo = array();

    public function beforeAction() {
        parent::beforeAction();

        $this->shopManager = Base::getObject('service.manager.shopManager');
        $this->shopInfo = $this->shopManager->setShopId($this->shopId)->getData('MAIN');
        if($this->shopInfo){
            $this->shopInfo['telphone'] = explode('/', $this->shopInfo['telphone']);
        }else{
            $this->renderMessage('店铺不存在或者已删除');
        }
    }

    public function indexAction() {
        $this->setVar('data', $this->shopInfo);
        $this->render('m_delivery');
    }

    public function saveAction() {
        if(!get_request('submitform')){
            header('Location: /account.php?m=biz&c=delivery');
        }

        $error = $this->check();

        if(!$error){
            $this->shopManager
                //->setShopId($this->shopId)
                 ->setField('telphone', trim(join('/', get_request('telphone')), '/'))
                 ->setField('delivery_desc', get_request('delivery_desc'))
                 ->setField('delivery_time', get_request('delivery_time'))
                 ->setField('min_charge', intval(get_request('min_charge')))
                 ->setField('min_order', intval(get_request('min_order')))
                 ->updateDb();
            $this->renderMessage('外卖信息编辑成功');
        }else{
            $this->setVar('error', $error);
            $this->setVar('data', get_request());
            $this->render('m_delivery');
        }
    }

    public function check() {
        //$shopManager = $this->getObject('service.manager.shopManager');
        $error = array();

        //送餐电话
        $telphones = get_request('telphone');
        $telnum = 0;;
        foreach($telphones as $tel){
            if($tel){
                if(Validate::isTelphone($tel)) {
                    $telnum ++;
                }else{
                    $error['telphone'] = '送餐电话号码格式错误';
                }
            }
        }
        if(!$telnum){
            $error['telphone'] or $error['telphone'] = '至少添加一部送餐电话';
        }

        //配送说明
        $desc = get_request('delivery_desc');
        if(iconv_strlen($desc) < 10 || iconv_strlen($desc) > 250){
            $error['delivery_desc'] = '配送说明保证在10～250个字符之间'.iconv_strlen($desc);
        }
        return $error;
    }
}
